<?php
namespace app\api\controller;

use app\admin\model\system\System;
use app\admin\model\teamrecord\Teamrecord;
use app\common\controller\Api;
use think\Db;
use think\Exception;

class Level extends Api{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**先锋**/
    public function xianfeng(){
        $userModel = new \app\admin\model\User();
        $levelModel = new \app\admin\model\level\Level();
        $levelinfo = $levelModel->where(['id'=>1])->find()->toArray();
        $withdrawModel = new \app\admin\model\withdraw\Withdraw();

        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();

        $rechargeModel = new \app\admin\model\recharge\Recharge();
        $where['zhituinums'] = ['>=',2];
        $where['team_amount'] = ['>=',$levelinfo['amount']];
        $list = collection($userModel->where($where)->where(['level'=>1])->select());
        $teamrecordModel = new Teamrecord();
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    if($v['team_amount'] > 0){
                        $one = $v['team_amount']/365*$levelinfo['bili']/100;
                        //修改用户收益和团队奖励
                        $userModel->where(['id'=>$v['id']])->update([
                            'shouyi_balance'=>$v['shouyi_balance']+$one,
                            'tuandui_amount'=>$v['tuandui_amount']+$one,
                            'today_team_shouyi'=>$one
                        ]);

                        //写入团队奖纪录
                        $indata['user_id'] = $v['id'];
                        $indata['team_amount'] = $v['team_amount']/365;
                        $indata['amount'] = $one;
                        $indata['bili']=$levelinfo['bili'];
                        $indata['event']='用户'.$v['nickname'].'等级达到战士获取团队奖励'.$one;
                        $indata['create_time'] = time();
                        $teamrecordModel->insert($indata);
                    }
                }
                Db::commit();
                $this->success('发放团队收益成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
    }

    /**战士**/
    public function zhanshi(){
        $userModel = new \app\admin\model\User();
        $levelModel = new \app\admin\model\level\Level();
        $levelinfo = $levelModel->where(['id'=>2])->find()->toArray();
        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();

        $withdrawModel = new \app\admin\model\withdraw\Withdraw();
        $where['zhituinums'] = ['>=',3];
        $where['team_amount'] = ['>=',$levelinfo['amount']];
        $list = collection($userModel->where($where)->where(['level'=>2])->select());
        $teamrecordModel = new Teamrecord();
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    if($v['team_amount'] > 0 ){
                        $one = $v['team_amount']/365*$levelinfo['bili']/100;
                        //修改用户收益和团队奖励
                        $userModel->where(['id'=>$v['id']])->update([
                            'shouyi_balance'=>$v['shouyi_balance']+$one,
                            'tuandui_amount'=>$v['tuandui_amount']+$one,
                            'today_team_shouyi'=>$one
                        ]);

                        //写入团队奖纪录
                        $indata['user_id'] = $v['id'];
                        $indata['team_amount'] = $v['team_amount']/365;
                        $indata['amount'] = $one;
                        $indata['bili']=$levelinfo['bili'];
                        $indata['event']='用户'.$v['nickname'].'等级达到战士获取团队奖励'.$one;
                        $indata['create_time'] = time();
                        $teamrecordModel->insert($indata);
                    }
                }
                Db::commit();
                $this->success('发放团队收益成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
    }

    /**精英**/
    public function jinyin(){
        $userModel = new \app\admin\model\User();
        $levelModel = new \app\admin\model\level\Level();
        $levelinfo = $levelModel->where(['id'=>3])->find()->toArray();

        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();

        $withdrawModel = new \app\admin\model\withdraw\Withdraw();
        $rechargeModel = new \app\admin\model\recharge\Recharge();
        $where['zhituinums'] = ['>=',3];
        $where['team_amount'] = ['>=',$levelinfo['amount']];
        $list = collection($userModel->where($where)->where(['level'=>3])->select());
        $teamrecordModel = new Teamrecord();
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    //精英必须邀请三个及以上战士
                    $inwhere['level']=['>=',2];
                    $invitenum = $userModel->where("FIND_IN_SET('{$v['id']}', pids)")->where($inwhere)->count();
                    if($invitenum>=3 && $v['team_amount']>0){
                        $one = $v['team_amount']/365*0.02;
                        //修改用户收益和团队奖励
                        $userModel->where(['id'=>$v['id']])->update([
                            'shouyi_balance'=>$v['shouyi_balance']+$one,
                            'tuandui_amount'=>$v['tuandui_amount']+$one,
                            'today_team_shouyi'=>$one
                        ]);

                        //写入团队奖纪录
                        $indata['user_id'] = $v['id'];
                        $indata['team_amount'] = $v['team_amount']/365;
                        $indata['amount'] = $one;
                        $indata['bili']=2;
                        $indata['event']='用户'.$v['nickname'].'等级达到精英获取团队奖励'.$one;
                        $indata['create_time'] = time();
                        $teamrecordModel->insert($indata);

                        $info = $userModel->where(['id'=>$v['id']])->find()->toArray();
                        $level = $levelModel->where(['id'=>$v['level']])->find()->toArray();
                        if($info['pids']!=''){
                            $idarr=explode(',',$info['pids']);
                            $idarr=array_reverse($idarr);
                            foreach ($idarr as $k1=>$v1){
                                $pinfo = $userModel->where(['id'=>$v1])->find();
                                if($pinfo){
                                    $pinfo = $pinfo->toArray();
                                    if($pinfo['level']>0){
                                        $plevelinfo =  $levelModel->where(['id'=>$pinfo['level']])->find()->toArray();
                                        if($pinfo['level']<3){

                                        }else{
                                            if($pinfo['level']<=$info['level']){
                                                $pamount = $info['today_shouyi']*0.1;
                                                if($pamount>0){
                                                    //上级用户增加余额
                                                    $userModel->where(['id'=>$pinfo['id']])->update([
                                                        'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                        'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                    ]);
                                                    //写团队奖纪录
                                                    $indata2['user_id'] = $pinfo['id'];
                                                    $indata2['team_amount'] = $info['today_shouyi'];
                                                    $indata2['amount'] = $pamount;
                                                    $indata2['bili'] = 10;
                                                    $indata2['event'] = '用户'.$pinfo['nickname'].'不满足极差条件拿下家用户'.$info['nickname'].'日收益的10%为'.$pamount;
                                                    $indata2['create_time'] = time();
                                                    $teamrecordModel->insert($indata2);
                                                }
                                            }else{
                                                //计算极差
                                                $cha = $plevelinfo['bili']-$level['bili'];
                                                $pamount = $v['team_amount']/365*$cha/100;
                                                if($pamount>0){
                                                    //上级用户增加余额
                                                    $userModel->where(['id'=>$pinfo['id']])->update([
                                                        'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                        'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                    ]);
                                                    //写团队奖纪录
                                                    $indata2['user_id'] = $pinfo['id'];
                                                    $indata2['team_amount'] = $v['team_amount']/365;
                                                    $indata2['amount'] = $pamount;
                                                    $indata2['bili'] = $cha;
                                                    $indata2['event'] = '用户'.$pinfo['nickname'].'满足极差条件拿用户'.$info['nickname'].'团队当日充值额的'.$cha.'%为'.$pamount;
                                                    $indata2['create_time'] = time();
                                                    $teamrecordModel->insert($indata2);
                                                }
                                            }
                                        }
                                        $info = $pinfo;
                                        $level = $plevelinfo;
                                    }
                                }
                            }
                        }
                    }
                }
                Db::commit();
                $this->success('发放团队收益成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
    }

    /**将领**/
    public function jianglin(){
        $userModel = new \app\admin\model\User();
        $levelModel = new \app\admin\model\level\Level();
        $levelinfo = $levelModel->where(['id'=>4])->find()->toArray();

        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();
        $withdrawModel = new \app\admin\model\withdraw\Withdraw();
        $rechargeModel = new \app\admin\model\recharge\Recharge();
        $where['zhituinums'] = ['>=',3];
        $where['team_amount'] = ['>=',$levelinfo['amount']];
        $list = collection($userModel->where($where)->where(['level'=>4])->select());
        $teamrecordModel = new Teamrecord();
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    //将领必须邀请三个及以上精英
                    $inwhere['level']=['>=',3];
                    $invitenum = $userModel->where("FIND_IN_SET('{$v['id']}', pids)")->where($inwhere)->count();
                    if($v['team_amount']>0 && $invitenum>=3) {
                        $one = $v['team_amount']/365*0.02;
                        //修改用户收益和团队奖励
                        $userModel->where(['id'=>$v['id']])->update([
                            'shouyi_balance'=>$v['shouyi_balance']+$one,
                            'tuandui_amount'=>$v['tuandui_amount']+$one,
                            'today_team_shouyi'=>$one
                        ]);

                        //写入团队奖纪录
                        $indata['user_id'] = $v['id'];
                        $indata['team_amount'] = $v['team_amount']/365;
                        $indata['amount'] = $one;
                        $indata['bili']=2;
                        $indata['event']='用户'.$v['nickname'].'等级达到将领获取团队奖励'.$one;
                        $indata['create_time'] = time();
                        $teamrecordModel->insert($indata);

                        $info = $userModel->where(['id'=>$v['id']])->find()->toArray();
                        $level = $levelModel->where(['id'=>$v['level']])->find()->toArray();
                        if($info['pids']!=''){
                            $idarr=explode(',',$info['pids']);
                            $idarr=array_reverse($idarr);
                            foreach ($idarr as $k1=>$v1){
                                $pinfo = $userModel->where(['id'=>$v1])->find();
                                if($pinfo){
                                    $pinfo = $pinfo->toArray();
                                    if($pinfo['level']>0){
                                        $plevelinfo =  $levelModel->where(['id'=>$pinfo['level']])->find()->toArray();
                                        if($pinfo['level']<3){

                                        }else{
                                            if($pinfo['level']<=$info['level']){
                                                $pamount = $info['today_shouyi']*0.1;
                                                if($pamount>0){
                                                    //上级用户增加余额
                                                    $userModel->where(['id'=>$pinfo['id']])->update([
                                                        'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                        'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                    ]);
                                                    //写团队奖纪录
                                                    $indata2['user_id'] = $pinfo['id'];
                                                    $indata2['team_amount'] = $info['today_shouyi'];
                                                    $indata2['amount'] = $pamount;
                                                    $indata2['bili'] = 10;
                                                    $indata2['event'] = '用户'.$pinfo['nickname'].'不满足极差条件拿下家用户'.$info['nickname'].'日收益的10%为'.$pamount;
                                                    $indata2['create_time'] = time();
                                                    $teamrecordModel->insert($indata2);
                                                }
                                            }else{
                                                //计算极差
                                                $cha = $plevelinfo['bili']-$level['bili'];
                                                $pamount = $v['team_amount']/365*$cha/100;
                                                if($pamount>0){
                                                    //上级用户增加余额
                                                    $userModel->where(['id'=>$pinfo['id']])->update([
                                                        'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                        'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                    ]);
                                                    //写团队奖纪录
                                                    $indata2['user_id'] = $pinfo['id'];
                                                    $indata2['team_amount'] = $v['team_amount']/365;
                                                    $indata2['amount'] = $pamount;
                                                    $indata2['bili'] = $cha;
                                                    $indata2['event'] = '用户'.$pinfo['nickname'].'满足极差条件拿用户'.$info['nickname'].'团队当日充值额的'.$cha.'%为'.$pamount;
                                                    $indata2['create_time'] = time();
                                                    $teamrecordModel->insert($indata2);
                                                }
                                            }
                                        }
                                        $info = $pinfo;
                                        $level = $plevelinfo;
                                    }
                                }
                            }
                        }
                    }
                }
                Db::commit();
                $this->success('发放团队收益成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
    }

    /**司令**/
    public function siling(){
        $userModel = new \app\admin\model\User();
        $levelModel = new \app\admin\model\level\Level();
        $levelinfo = $levelModel->where(['id'=>5])->find()->toArray();

        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();
        $withdrawModel = new \app\admin\model\withdraw\Withdraw();
        $rechargeModel = new \app\admin\model\recharge\Recharge();
        $where['zhituinums'] = ['>=',3];
        $where['team_amount'] = ['>=',$levelinfo['amount']];
        $list = collection($userModel->where($where)->where(['level'=>5])->select());
        $teamrecordModel = new Teamrecord();
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    //司令必须邀请三个及以上将领
                    $inwhere['level']=['>=',4];
                    $invitenum = $userModel->where("FIND_IN_SET('{$v['id']}', pids)")->where($inwhere)->count();
                    if($invitenum>=3 && $v['team_amount']>0){
                        $one = $v['team_amount']/365*0.02;
                        //修改用户收益和团队奖励
                        $userModel->where(['id'=>$v['id']])->update([
                            'shouyi_balance'=>$v['shouyi_balance']+$one,
                            'tuandui_amount'=>$v['tuandui_amount']+$one,
                            'today_team_shouyi'=>$one
                        ]);

                        //写入团队奖纪录
                        $indata['user_id'] = $v['id'];
                        $indata['team_amount'] = $v['team_amount']/365;
                        $indata['amount'] = $one;
                        $indata['bili']=2;
                        $indata['event']='用户'.$v['nickname'].'等级达到司令获取团队奖励'.$one;
                        $indata['create_time'] = time();
                        $teamrecordModel->insert($indata);

                        $info = $userModel->where(['id'=>$v['id']])->find()->toArray();
                        $level = $levelModel->where(['id'=>$v['level']])->find()->toArray();
                        if($info['pids']!=''){
                            $idarr=explode(',',$info['pids']);
                            $idarr=array_reverse($idarr);
                            foreach ($idarr as $k1=>$v1){
                                $pinfo = $userModel->where(['id'=>$v1])->find();
                                if($pinfo){
                                    $pinfo = $pinfo->toArray();
                                    if($pinfo['level']>0){
                                        $plevelinfo =  $levelModel->where(['id'=>$pinfo['level']])->find()->toArray();
                                        if($pinfo['level']<3){

                                        }else{
                                            if($pinfo['level']<=$info['level']){
                                                $pamount = $info['today_shouyi']*0.1;
                                                if($pamount>0){
                                                    //上级用户增加余额
                                                    $userModel->where(['id'=>$pinfo['id']])->update([
                                                        'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                        'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                    ]);
                                                    //写团队奖纪录
                                                    $indata2['user_id'] = $pinfo['id'];
                                                    $indata2['team_amount'] = $info['today_shouyi'];
                                                    $indata2['amount'] = $pamount;
                                                    $indata2['bili'] = 10;
                                                    $indata2['event'] = '用户'.$pinfo['nickname'].'不满足极差条件拿下家用户'.$info['nickname'].'日收益的10%为'.$pamount;
                                                    $indata2['create_time'] = time();
                                                    $teamrecordModel->insert($indata2);
                                                }
                                            }else{
                                                //计算极差
                                                $cha = $plevelinfo['bili']-$level['bili'];
                                                $pamount = $v['team_amount']/365*$cha/100;
                                                if($pamount>0){
                                                    //上级用户增加余额
                                                    $userModel->where(['id'=>$pinfo['id']])->update([
                                                        'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                        'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                    ]);
                                                    //写团队奖纪录
                                                    $indata2['user_id'] = $pinfo['id'];
                                                    $indata2['team_amount'] = $v['team_amount']/365;
                                                    $indata2['amount'] = $pamount;
                                                    $indata2['bili'] = $cha;
                                                    $indata2['event'] = '用户'.$pinfo['nickname'].'满足极差条件拿用户'.$info['nickname'].'团队当日充值额的'.$cha.'%为'.$pamount;
                                                    $indata2['create_time'] = time();
                                                    $teamrecordModel->insert($indata2);
                                                }
                                            }
                                        }
                                        $info = $pinfo;
                                        $level = $plevelinfo;
                                    }
                                }
                            }
                        }
                    }
                }
                Db::commit();
                $this->success('发放团队收益成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
    }

    /**统帅**/
    public function tongshuai(){
        $userModel = new \app\admin\model\User();
        $levelModel = new \app\admin\model\level\Level();
        $levelinfo = $levelModel->where(['id'=>6])->find()->toArray();

        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();
        $withdrawModel = new \app\admin\model\withdraw\Withdraw();
        $rechargeModel = new \app\admin\model\recharge\Recharge();
        $where['zhituinums'] = ['>=',3];
        $where['team_amount'] = ['>=',$levelinfo['amount']];
        $list = collection($userModel->where($where)->where(['level'=>6])->select());
        $teamrecordModel = new Teamrecord();
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    //统帅必须邀请三个及以上司令
                    $inwhere['level']=['>=',5];
                    $invitenum = $userModel->where("FIND_IN_SET('{$v['id']}', pids)")->where($inwhere)->count();
                    if($v['team_amount']>0 && $invitenum>=3){
                        $one = $v['team_amount']/365*0.03;
                        //修改用户收益和团队奖励
                        $userModel->where(['id'=>$v['id']])->update([
                            'shouyi_balance'=>$v['shouyi_balance']+$one,
                            'tuandui_amount'=>$v['tuandui_amount']+$one,
                            'today_team_shouyi'=>$one
                        ]);

                        //写入团队奖纪录
                        $indata['user_id'] = $v['id'];
                        $indata['team_amount'] = $v['team_amount']/365;
                        $indata['amount'] = $one;
                        $indata['bili']=3;
                        $indata['event']='用户'.$v['nickname'].'等级达到统帅获取团队奖励'.$one;
                        $indata['create_time'] = time();
                        $teamrecordModel->insert($indata);

                        $info = $userModel->where(['id'=>$v['id']])->find()->toArray();
                        $level = $levelModel->where(['id'=>$v['level']])->find()->toArray();
                        if($info['pids']!=''){
                            $idarr=explode(',',$info['pids']);
                            $idarr=array_reverse($idarr);
                            foreach ($idarr as $k1=>$v1){
                                $pinfo = $userModel->where(['id'=>$v1])->find();
                                if($pinfo){
                                    $pinfo = $pinfo->toArray();
                                    if($pinfo['level'] > 0){
                                        $plevelinfo =  $levelModel->where(['id'=>$pinfo['level']])->find()->toArray();
                                        if($pinfo['level']<3){

                                        }else{
                                            if($pinfo['level']<=$info['level']){
                                                $pamount = $info['today_shouyi']*0.1;
                                                if($pamount>0){
                                                    //上级用户增加余额
                                                    $userModel->where(['id'=>$pinfo['id']])->update([
                                                        'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                        'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                    ]);
                                                    //写团队奖纪录
                                                    $indata2['user_id'] = $pinfo['id'];
                                                    $indata2['team_amount'] = $info['today_shouyi'];
                                                    $indata2['amount'] = $pamount;
                                                    $indata2['bili'] = 10;
                                                    $indata2['event'] = '用户'.$pinfo['nickname'].'不满足极差条件拿下家用户'.$info['nickname'].'日收益的10%为'.$pamount;
                                                    $indata2['create_time'] = time();
                                                    $teamrecordModel->insert($indata2);
                                                }
                                            }else{
                                                //计算极差
                                                $cha = $plevelinfo['bili']-$level['bili'];
                                                $pamount = $v['team_amount']/365*$cha/100;
                                                if($pamount>0){
                                                    //上级用户增加余额
                                                    $userModel->where(['id'=>$pinfo['id']])->update([
                                                        'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                        'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                    ]);
                                                    //写团队奖纪录
                                                    $indata2['user_id'] = $pinfo['id'];
                                                    $indata2['team_amount'] = $v['team_amount']/365;
                                                    $indata2['amount'] = $pamount;
                                                    $indata2['bili'] = $cha;
                                                    $indata2['event'] = '用户'.$pinfo['nickname'].'满足极差条件拿用户'.$info['nickname'].'团队当日充值额的'.$cha.'%为'.$pamount;
                                                    $indata2['create_time'] = time();
                                                    $teamrecordModel->insert($indata2);
                                                }
                                            }
                                        }
                                        $info = $pinfo;
                                        $level = $plevelinfo;
                                    }
                                }
                            }
                        }
                    }
                }
                Db::commit();
                $this->success('发放团队收益成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
    }

    public function xiaozhengteam(){
        $userModel = new \app\admin\model\User();
        $rechargeModel = new \app\admin\model\recharge\Recharge();
        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();
        $list = collection($userModel->where([])->select());
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    $childlist = collection($userModel->where("FIND_IN_SET('{$v['id']}', pids)")->select());
                    if($childlist){
                        $childlist = $childlist->toArray();
                        $idarr=[];
                        foreach ($childlist as $k1=>$v1){
                            array_push($idarr,$v1['id']);
                        }
                        $idstr = implode(",",$idarr);
                        $rwhere['user_id'] = ['in',$idstr];
                        $all_amount = $rechargeModel->where($rwhere)->where(['status'=>1])->sum('amount');
                        $all_amount = $all_amount*$systeminfo['bili'];
                        $userModel->where(['id'=>$v['id']])->update([
                            'team_amount'=>$all_amount,
                        ]);
                    }
                }
                Db::commit();
                $this->success('校正团队额成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }else{
            $this->success('没有校正团队额的数据');
        }
    }

    public function xiaozhenglevel(){
        $userModel = new \app\admin\model\User();
        $list = collection($userModel->where([])->select());
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    if($v['team_amount']>=10000 && $v['zhituinums']>=2){
                        $userModel->where(['id'=>$v['id']])->update(['level'=>1]);
                    }elseif ($v['team_amount']>=50000 && $v['zhituinums']>=3){
                        $userModel->where(['id'=>$v['id']])->update(['level'=>2]);
                    }elseif ($v['team_amount']>=200000 && $v['zhituinums']>=3){
                        $lwhere['level']=['>=',2];
                        $num  = $userModel->where($lwhere)->where(['pid'=>$v['id']])->count();
                        if($num>=3){
                            $userModel->where(['id'=>$v['id']])->update(['level'=>3]);
                        }
                    }elseif ($v['team_amount']>=1250000 && $v['zhituinums']>=3){
                        $lwhere['level']=['>=',3];
                        $num  = $userModel->where($lwhere)->where(['pid'=>$v['id']])->count();
                        if($num>=3){
                            $userModel->where(['id'=>$v['id']])->update(['level'=>4]);
                        }
                    }elseif ($v['team_amount']>=6250000 && $v['zhituinums']>=3){
                        $lwhere['level']=['>=',4];
                        $num  = $userModel->where($lwhere)->where(['pid'=>$v['id']])->count();
                        if($num>=3){
                            $userModel->where(['id'=>$v['id']])->update(['level'=>5]);
                        }
                    }elseif ($v['team_amount']>=31250000 && $v['zhituinums']>=3){
                        $lwhere['level']=['>=',5];
                        $num  = $userModel->where($lwhere)->where(['pid'=>$v['id']])->count();
                        if($num>=3){
                            $userModel->where(['id'=>$v['id']])->update(['level'=>6]);
                        }
                    }else{

                    }
                }
                Db::commit();
                $this->success('校正等级成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }else{
            $this->success('没有需要校正的等级');
        }
    }

}